//
//  StringPredicate.m
//  oilsFavorable
//
//  Created by 谷峰 on 2017/5/23.
//  Copyright © 2017年 DDSB. All rights reserved.
//

#import "StringPredicate.h"

@implementation StringPredicate

+ (BOOL)isBlankString:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES;
    }

    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }

    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        return YES;
    }

    if ([string isEqualToString:@"请选择"]) {
        return YES;
    }

    if ([string isEqualToString:@"必填"]) {
        return YES;
    }

    if ([string isEqualToString:@"请填写"]) {
        return YES;
    }
    
    return NO;
}

+ (BOOL)regResultWithString:(NSString *)string
{
    if ([self isBlankString:string]) {
        return NO;
    }
    
    NSString *regex = @"^1[0-9]{10}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:string];
    return isMatch;
}

+ (BOOL)checkUserNickName:(NSString *)string
{
    NSString *regex = @"^[a-zA-Z0-9\u4e00-\u9fa5]{0,6}+$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:string];
    return isMatch;
}

+ (BOOL)validateEmail:(NSString *)email
{
    if ([self isBlankString:email]) {
        return NO;
    }
    NSString *emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

+ (BOOL)chackLoginPassword:(NSString *)password{
    if ([self isBlankString:password]) {
        return NO;
    }
    NSString *passwordPred = @"(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,}";
    NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordPred];
    return [passwordTest evaluateWithObject:password];
}
//求职端登录密码正则
+ (BOOL)ApplicantLoginPassword:(NSString *)password{
    if ([self isBlankString:password]) {
        return NO;
    }
    NSString *passwordPred = @"(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}";
    NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordPred];
    return [passwordTest evaluateWithObject:password];
}

+ (BOOL)JudgeTheillegalCharacter:(NSString *)content{
    //提示 标签不能输入特殊字符
    NSString *str =@"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    if (![emailTest evaluateWithObject:content]) {
        return YES;
    }
    return NO;
}
//纯数字
+ (BOOL)isPureFloat:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return [scan scanFloat:&val] && [scan isAtEnd];
}
//输入不能大于多少位
+ (BOOL)JudgeStringLengthIsbiger:(NSString*)strtemp {
    int strlength = 0;
    char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else {
            p++;
        }
    }
    
    int result = (strlength+1)/2;
    if (result>20) {
        return YES;
    }
    return NO;
}

+ (NSString*)hanziToPinYin:(NSString*)str{
     //转成可变字符串
    NSMutableString *tempStr1 = [NSMutableString stringWithString:str];
    //先转换为带声调的拼音
    CFStringTransform((CFMutableStringRef)tempStr1, NULL, kCFStringTransformToLatin, false);
    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)tempStr1, NULL, kCFStringTransformStripDiacritics, false);
    NSLog(@"汉字转拼音：tempStr1 == %@", tempStr1);
    // ni hao
    tempStr1 = (NSMutableString *)tempStr1.uppercaseString;
    NSLog(@"小写转大写：tempStr1 == %@", tempStr1);
    
    return tempStr1;
}
//身份证号
+ (BOOL)isValidCarID:(NSString *)carID {
    if (carID.length!=7) {
        return NO;
    }
    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-hj-zA-HJ-Z]{1}[a-hj-zA-HJ-Z_0-9]{4}[a-hj-zA-HJ-Z_0-9_\u4e00-\u9fa5]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    return [carTest evaluateWithObject:carID];
    
    return YES;
}
+ (NSString *)networkingStatesFromStatebar {
    // 状态栏是由当前app控制的，首先获取当前app
    UIApplication *app = [UIApplication sharedApplication];
    
    NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews];
    
    int type = 0;
    for (id child in children) {
        if ([child isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            type = [[child valueForKeyPath:@"dataNetworkType"] intValue];
        }
    }
    
    NSString *stateString = @"notReachable";
    
    switch (type) {
        case 0:
            stateString = @"notReachable";
            break;
            
        case 1:
            stateString = @"2G";
            break;
            
        case 2:
            stateString = @"3G";
            break;
            
        case 3:
            stateString = @"4G";
            break;
            
        case 4:
            stateString = @"LTE";
            break;
            
        case 5:
            stateString = @"wifi";
            break;
            
        default:
            break;
    }
    
    return stateString;
}
@end
